﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Exceptions;
using gdlmo.Common;
using gdlmo.DNN.Modules.Informations.Business;

namespace gdlmo.DNN.Modules.Informations
{
	public abstract class InformationsEdit : PortalModuleBase
	{

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{			
			this.cmdUpdate.Click += new System.EventHandler(this.cmdUpdate_Click);
			this.cmdCancel.Click += new System.EventHandler(this.cmdCancel_Click);
			this.cmdDelete.Click += new System.EventHandler(this.cmdDelete_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		#region Controls
		protected System.Web.UI.WebControls.DropDownList dlsDept;		
		protected System.Web.UI.WebControls.TextBox txtPublisher;
		protected System.Web.UI.WebControls.TextBox txtTitle;
		protected System.Web.UI.WebControls.LinkButton cmdUpdate;
		protected System.Web.UI.WebControls.LinkButton cmdCancel;
		protected System.Web.UI.WebControls.LinkButton cmdDelete;
		protected DotNetNuke.UI.UserControls.TextEditor teDescription;
		#endregion		
		
		#region Private Function
		protected void BinderDepts()
		{			
			dlsDept.DataTextField = "Name";
			dlsDept.DataValueField = "Name";
			dlsDept.DataSource = CommonFunc.GetDepts();
			dlsDept.DataBind();
		}
		#endregion

		#region Private Members
		private int InfoId;
		private DateTime CreateDate
		{
			get 
			{
				object o = ViewState["CreateDate"];
				if (o == null) 
				{
					return DateTime.Now;
				}
				return (DateTime)o;
			}

			set 
			{				
				ViewState["CreateDate"] = value;
			}
		}	
		#endregion
		
		#region Event Handlers
		private void Page_Load(object sender, System.EventArgs e)
		{
			try 
			{
				InformationsController objCtlInformations = new InformationsController();
				InformationsInfo objInformations = new InformationsInfo();
				if (!(Request.Params["InfoId"] == null)) 
				{
					InfoId = Int32.Parse(Request.Params["InfoId"]);
				} 
				else 
				{
					InfoId = Null.NullInteger;
				}
				if (!Page.IsPostBack) 
				{
					cmdDelete.Attributes.Add("onClick", "javascript:return confirm('" + Localization.GetString("DeleteItem") + "');");
					BinderDepts();
					if (!Null.IsNull(InfoId)) 
					{
						objInformations = objCtlInformations.Get(InfoId, ModuleId);
						if (objInformations != null) 
						{
							cmdDelete.Visible = true;
							txtTitle.Text = objInformations.Title;
							teDescription.Text = objInformations.Description;
							txtPublisher.Text = objInformations.Publisher;
							CommonFunc.GetSelectValue(dlsDept.Items,objInformations.DeptName);		//当部门不再为活动时，添加到选择列表
							dlsDept.SelectedValue = objInformations.DeptName;
							CreateDate = objInformations.CreateDate;
						} 
						else 
						{
							Response.Redirect(Globals.NavigateURL(), true);
						}
					}
				}
			} 
			catch (Exception exc) 
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}

		private void cmdUpdate_Click(object sender, EventArgs e)
		{
			try 
			{
				if (Page.IsValid == true) 
				{
					InformationsInfo objInformations = new InformationsInfo();
					objInformations = ((InformationsInfo)CBO.InitializeObject(objInformations, typeof(InformationsInfo)));
					objInformations.InfoId = InfoId;
					objInformations.ModuleId = ModuleId;
					objInformations.Title = txtTitle.Text;
					objInformations.Description = teDescription.Text;
					objInformations.Publisher = txtPublisher.Text;
					objInformations.CreateDate = CreateDate;
					objInformations.ExpireDate = DateTime.Now;
					objInformations.PublishDate = DateTime.Now;
					objInformations.DeptName = dlsDept.SelectedValue.ToString();
					objInformations.CreatedByUser = UserId.ToString();
					InformationsController objCtlInformations = new InformationsController();

					if (Null.IsNull(InfoId)) 
					{				
						//objInformations.CreateDate = DateTime.Now;
						objCtlInformations.Add(objInformations);						
					} 
					else 
					{
						objCtlInformations.Update(objInformations);
					}
					Response.Redirect(Globals.NavigateURL(), true);
				}
			} 
			catch (Exception exc) 
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}

		private void cmdCancel_Click(object sender, EventArgs e)
		{
			try 
			{
				Response.Redirect(Globals.NavigateURL(), true);
			} 
			catch (Exception exc) 
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}

		private void cmdDelete_Click(object sender, EventArgs e)
		{
			try 
			{
				if (!Null.IsNull(InfoId)) 
				{
					InformationsController objCtlInformations = new InformationsController();
					objCtlInformations.Delete(InfoId);
				}
				Response.Redirect(Globals.NavigateURL(), true);
			} 
			catch (Exception exc) 
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		#endregion
	}
}